<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
            <el-form-item label="操作人" prop="createBy">
                <el-select
                    v-model="queryParams.userName"
                    placeholder="请输入操作人"
                    clearable
                    size="small"
                    style="width: 240px"
                >
                    <el-option
                        v-for="dict in userList"
                        :key="dict.userName"
                        :label="dict.nickName"
                        :value="dict.userName"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="保存时间">
                <el-date-picker v-model="dateRange" size="small" style="width: 360px" value-format="yyyy-MM-dd HH:mm:ss"
                                type="datetimerange" range-separator="-" :default-time="['00:00:00', '23:59:59']" start-placeholder="开始日期" end-placeholder="结束日期"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                           v-hasPermi="['bs:data:saveOrUpdate']"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                        v-hasPermi="['bs:data:export']"
                >导出</el-button>
            </el-col>
        </el-row>
        <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
            <el-table-column label="保存时间" align="center" prop="createTime" width="180" />
            <el-table-column label="开屏展示量" align="center" prop="kaiping" :show-overflow-tooltip="true" />
            <el-table-column label="激励视频展示量" align="center" prop="jili" :show-overflow-tooltip="true" />
            <el-table-column label="预估收益" align="center" prop="yugu" :show-overflow-tooltip="true" />
            <el-table-column label="所属渠道" align="center" prop="belongTag" width="100" :formatter="statusFormat"/>
            <el-table-column label="操作人" align="center" prop="createBy" :show-overflow-tooltip="true" />
            <el-table-column label="ip地址" align="center" prop="ipAddress" :show-overflow-tooltip="true" >
                <template slot-scope="scope">
                    {{scope.row.ipAddress === null ? "-" : scope.row.ipAddress}}
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.nowPage" :limit.sync="queryParams.pageSize" @pagination="getPage"/>
        <!-- 查看数据详情对话框 -->
        <el-dialog :title="title" :visible.sync="showOpen" width="800px"  v-dialog-drag :close-on-click-modal='false'>
            <el-form ref="showForm" :model="showForm" :rules="rules" label-width="120px" >
                <el-form-item label="开屏展示量" prop="kaiping">
                    <el-input v-model="showForm.kaiping" style="width: 450px;"  type="text"  disabled placeholder="请输入开屏展示量" />
                </el-form-item>
                <el-form-item label="激励视频展示量" prop="jili">
                    <el-input v-model="showForm.jili" style="width: 450px;"  type="text"  disabled placeholder="请输入激励视频展示量" />
                </el-form-item>
                <el-form-item label="预估收益" prop="yugu">
                    <el-input v-model="showForm.yugu" style="width: 450px;"  type="text"  disabled placeholder="请输入预估收益" />
                </el-form-item>
                <el-form-item label="所属渠道" prop="belongTag">
                    <el-select
                        v-model="showForm.belongTag"
                        placeholder="所属渠道"
                        clearable
                        style="width: 450px"
                        disabled="true"
                        size="small">
                        <el-option
                            v-for="dict in belong_tag"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="ip地址" prop="ipAddress">
                    <el-input v-model="showForm.ipAddress" style="width: 450px;"  type="text"  disabled placeholder="请输入ip地址" />
                </el-form-item>
                <el-form-item label="创建时间" prop="createTime">
                    <el-date-picker type="datetime" v-model="showForm.createTime" format="yyyy-MM-dd HH:mm:ss"
                                    value-format="yyyy-MM-dd HH:mm:ss" :style="{width: '100%'}" disabled placeholder="请选择创建时间" clearable>
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="创建人" prop="createBy">
                    <el-input v-model="showForm.createBy" style="width: 450px;"  type="text"  disabled placeholder="请输入创建人" />
                </el-form-item>
                <el-form-item label="更新时间" prop="updateTime">
                    <el-date-picker type="datetime" v-model="showForm.updateTime" format="yyyy-MM-dd HH:mm:ss"
                                    value-format="yyyy-MM-dd HH:mm:ss" :style="{width: '100%'}" disabled placeholder="请选择更新时间" clearable>
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="更新人" prop="updateBy">
                    <el-input v-model="showForm.updateBy" style="width: 450px;"  type="text"  disabled placeholder="请输入更新人" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelShow">关闭</el-button>
            </div>
        </el-dialog>
        <!-- 添加或修改数据对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px"  v-dialog-drag :close-on-click-modal='false'>
            <el-form ref="form" :model="form" :rules="rules" label-width="140px">
                <el-form-item label="开屏展示量" prop="kaiping">
                    <el-input v-model="form.kaiping" style="width: 450px;"  type="text" placeholder="请输入开屏展示量" />
                </el-form-item>
                <el-form-item label="激励视频展示量" prop="jili">
                    <el-input v-model="form.jili" style="width: 450px;"  type="text" placeholder="请输入激励视频展示量" />
                </el-form-item>
                <el-form-item label="预估收益" prop="yugu">
                    <el-input v-model="form.yugu" style="width: 450px;"  type="text" placeholder="请输入预估收益" />
                </el-form-item>
                <el-form-item label="所属渠道" prop="belongTag">
                    <el-select
                        v-model="form.belongTag"
                        placeholder="所属渠道"
                        clearable
                        style="width: 450px"
                        size="small">
                        <el-option
                            v-for="dict in belong_tag"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                        />
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import {getBsDataPage,getBsDataById,addOrUpdateBsData,delBsData,exportBsData} from "@/api/project/bsData/bsData";
import {listUser} from "@/api/system/user";
export default {
    name: "bsData",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 参数表格数据
            configList: [],
            // 弹出层标题
            title: "",
            open: false,// 是否显示修改或增加的弹出层
            // 日期范围
            dateRange: [],
            // 人员列表
            userList: [],
            // 所属渠道
            belong_tag: [],
            // 查询参数
            queryParams: {
                nowPage: 1,
                pageSize: 10,
                userName: undefined,
            },
            // 表单参数
            form: {},
            showOpen: false,//是否显示查看工作内容弹出层
            showForm: {},
            // 表单校验
            rules: {
                kaiping: [
                    {required: true, message: "开屏展示量不能为空", trigger: "blur"}
                ],
                jili: [
                    {required: true, message: "激励视频展示量不能为空", trigger: "blur"}
                ],
                yugu: [
                    {required: true, message: "预估收益不能为空", trigger: "blur"}
                ],
                ipAddress: [
                    {required: true, message: "ip地址不能为空", trigger: "blur"}
                ],
                createBy: [
                    {required: true, message: "创建人不能为空", trigger: "blur"}
                ],
                updateBy: [
                    {required: true, message: "更新人不能为空", trigger: "blur"}
                ],
            },
        };
    },
    created() {
        this.getPage();
        this.getUserList();
        // 所属渠道
        this.getDicts("belong_tag").then(response => {
            this.belong_tag = response.data;
        });
    },
    methods: {
        /** 查询参数列表 */
        getPage(e) {
            this.loading = true;
            let createBy = this.queryParams.userName || '';
            let time = this.dateRange || '';
            let params = '';
            if (e) {
                params = {
                    createBy: createBy,
                    startTime: time[0],
                    endTime: time[1],
                    nowPage: e.page,
                    pageSize: e.limit
                }
            } else {
                params = {
                    createBy: createBy,
                    startTime: time[0],
                    endTime: time[1],
                    nowPage: this.queryParams.nowPage,
                    pageSize: this.queryParams.pageSize
                }
            }
            getBsDataPage(params).then(response => {
                this.configList = response.data.records;
                this.total = response.data.total;
                this.loading = false;
            })
        },
        /**
         * 查询人员列表
         */
        getUserList() {
            this.loading = true;
            listUser().then(response => {
                this.userList = response.rows;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                kaiping: undefined,
                jili: undefined,
                yugu: undefined,
                ipAddress: undefined,
                createTime: undefined,
                createBy: undefined,
                updateTime: undefined,
                updateBy: undefined,
            };
            this.showForm = {
                kaiping: undefined,
                jili: undefined,
                yugu: undefined,
                ipAddress: undefined,
                createTime: undefined,
                createBy: undefined,
                updateTime: undefined,
                updateBy: undefined,
            };
            this.resetForm("form");
            this.resetForm("showForm");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            let createBy = this.queryParams.userName || '';
            let time = this.dateRange || '';
            let params = {
                createBy: createBy,
                startTime: time[0],
                endTime: time[1],
                nowPage: 1,
                pageSize: this.queryParams.pageSize
            };
            getBsDataPage(params).then(res => {
                if (res.code == 200) {
                    this.configList = res.data.records;
                    this.total = res.data.total;
                    this.loading = false;
                }
            })
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.queryParams.userName = '';
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加数据信息";
        },
        //多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id);
            this.single = selection.length != 1;
            this.multiple = !selection.length
        },
        /** 查看按钮操作 */
        handleShow(row) {
            this.reset();
            const id = row.id || '';
            getBsDataById({id}).then(response => {
                this.showForm = response.data;
                this.showOpen = true;
                this.title = "数据详情";
            });
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || '';
            getBsDataById({id}).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改数据信息";
            });
        },
        cancelShow: function () {
            this.showOpen = false;
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id !== undefined) {
                        addOrUpdateBsData(this.form).then(res => {
                            if (res.code == 200) {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getPage();
                            } else {
                                this.msgError(res.msg);
                            }
                        })
                    } else {
                        addOrUpdateBsData(this.form).then(res => {
                            if (res.code == 200) {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getPage();
                            } else {
                                this.msgError(res.msg);
                            }
                        })
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除数据编号为"' + ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delBsData(ids);
            }).then(() => {
                this.getPage();
                this.msgSuccess("删除成功");
            }).catch(function () {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出所有数据数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return exportBsData(queryParams);
            }).then(res => {
                this.download(res.msg);
            }).catch(function () {
            });
        },
        /**
         * 字典状态转换
         */
        statusFormat(row, column) {
            return this.selectDictLabel(this.belong_tag, row.belongTag);
        },
    }
};
</script>
<style>

</style>
